<?php
//пароль
if(!defined('INWEB')){header('Location: ../index.php'); die();}
$a=getVar('a');
includeLang('clients');
switch($a)
{
    case 'add':
        if($_POST)
        {
            if((!isset($_POST['name']) || $_POST['name']=='')&&(!isset($_POST['name2']) || $_POST['name2']=='')) {msg($Lang['error'], $Lang['nothing_to_add'],'error'); break;}
            if((!isset($_POST['fp_code']) || $_POST['fp_code']=='')&&(!isset($_POST['fp_code2']) || $_POST['fp_code2']=='')) {msg($Lang['error'], $Lang['nothing_to_add'],'error'); break;}
            
            $legal=isset($_POST['legal'])&&$_POST['legal']=='on'?true:false;
            $unwanted=isset($_POST['unwanted'])&&$_POST['unwanted']=='on'?true:false;
            if($legal)
            {
                $name=getVar('name2');
                $code=getVar('fp_code2','int');
                $reg_date=getVar('reg_date')==''?'null':'\''.getVar('reg_date').'\'';
                $fiscal=getVar('fiscal_code', 'int');
            }
            else
            {
                $name=getVar('name');
                $code=getVar('fp_code');
                $reg_date='null';
                $fiscal='';
            }
            $laddress=getVar('legal_address');
            $raddress=getVar('real_address');
            $phone=getVar('phone');
            $fax=getVar('fax');
            $mail=getVar('mail');
            $comment=getVar('comment');
            $legal=$legal==true?'1':'0';
            $unwanted=$unwanted==true?'1':'0';
            $sql->query("INSERT INTO clients (`name`, `address`, `legal`, `code`, `fiscal_code`, `reg_date`, `telephone`, `fax`, `e_mail`, `real_address`, `comment`, `unwanted`) VALUES
            ('$name','$laddress','$legal','$code','$fiscal',$reg_date,'$phone','$fax','$mail','$raddress','$comment','$unwanted');");
            $id=$sql->getId();
            if($sql->row_count)
                msg($Lang['success'], $Lang['client_added'].'<br /><a href="?p='.$page.'&a=view&id='.$id.'">'.$Lang['view_client'].'</a><br /><a href="?p='.$page.'&a=add">'.$Lang['add_another'].'</a>');
            else
                msg($Lang['warning'], $Lang['failed_to_add'], 'warning');
        }
        else
        {
            echo '<form action="?p='.$page.'&a=add" method="post" onsubmit="client_submit();"><table id="client" name="client" border="1">';
            echo '<tr><th>'.$Lang['name'].'</th><td><input type="text" id="name" name="name" value="" maxlength="50" /></td></tr>';
            echo '<tr style="display:none"><th>'.$Lang['title'].'</th><td><input type="text" id="name2" name="name2" value="" maxlength="50" /></td></tr>';
            echo '<tr><th>'.$Lang['legal'].'</th><td><input id="legal" name="legal" type="checkbox" onclick="toogle_client();" /></td></tr>';
            echo '<tr><th>'.$Lang['person_code'].'</th><td><input type="text" id="fp_code" name="fp_code" value="" maxlength="12" /></td></tr>';
            echo '<tr style="display:none"><th>'.$Lang['reg_nr'].'</th><td><input type="text" id="fp_code2" name="fp_code2" value="" maxlength="11" /></td></tr>';
            echo '<tr style="display:none"><th>'.$Lang['reg_date'].'</th><td><input type="text" name="reg_date" value="'.$obj['reg_date'].'" /></td></tr>';
            echo '<tr style="display:none"><th>'.$Lang['fiscal_code'].'</th><td><input type="text" name="fiscal_code" value="" maxlength="11" /></td></tr>';
            echo '<tr><th>'.$Lang['legal_address'].'</th><td><input type="text" id="legal_address" name="legal_address" value="" maxlength="60" onchange="write_real();" /></td></tr>';
            echo '<tr><th>'.$Lang['phone'].'</th><td><input type="text" name="phone" value="" maxlength="20" /></td></tr>';
            echo '<tr><th>'.$Lang['fax'].'</th><td><input type="text" name="fax" value="" maxlength="20" /></td></tr>';
            echo '<tr><th>'.$Lang['e_mail'].'</th><td><input type="text" name="mail" value="" maxlength="50" /></td></tr>';
            echo '<tr><th>'.$Lang['real_address'].'<input id="chbox_address" type="checkbox" title="Use Legal Address" onclick="toogle_address();" /></th><td><input type="text" id="real_address" name="real_address" value="" maxlength="60" /></td></tr>';
            echo '<tr><th>'.$Lang['comment'].'</th><td><input type="text" name="comment" value="" maxlength="255" /></td></tr>';
            echo '<tr><th>'.$Lang['unwanted'].'</th><td><input name="unwanted" type="checkbox" /></td></tr>';
            echo '</table><input type="submit" value="'.$Lang['add'].'" /></form>';
        }
    break;
    case 'view':
        if(!isset($_GET['id'])) break;
        $id=getVar('id');
        $qry=$sql->query("SELECT id, name, address, reg_date, real_address, telephone, fax, e_mail, real_address, unwanted, comment, IF(legal<>0,code,fiscal_code) as fp_code, IF(LENGTH(IF(legal<>0,code,fiscal_code))=12,'{$Lang['individualU']}','{$Lang['legalU']}') as ptype FROM clients WHERE clients.id='$id'");
        $obj=$sql->fetch_array($qry);
        echo '<table border="1">';
        $name=$obj['ptype']==$Lang['legalU']?$Lang['title']:$Lang['name'];
        $legal=$obj['ptype']==$Lang['legalU']?' checked="checked"':'';
        $code=$obj['ptype']==$Lang['legalU']?$Lang['reg_nr']:$Lang['person_code'];
        $unwanted=$obj['unwanted']=='1'?' checked="checked"':'';
        echo '<tr><th>'.$name.'</th><td>'.$obj['name'].'</td></tr>';
        echo '<tr><th>'.$Lang['legal'].'</th><td><input type="checkbox"'.$legal.' disabled="disabled" /></td></tr>';
        echo '<tr><th>'.$code.'</th><td>'.$obj['fp_code'].'</td></tr>';
        echo $obj['ptype']==$Lang['legalU']?'<tr><th>'.$Lang['reg_date'].'</th><td>'.$obj['reg_date'].'</td></tr><tr><th>'.$Lang['fiscal_code'].'</th><td>'.$obj['fiscal_code'].'</td></tr>':'';
        echo '<tr><th>'.$Lang['legal_address'].'</th><td>'.$obj['address'].'</td></tr>';
        echo '<tr><th>'.$Lang['phone'].'</th><td>'.$obj['telephone'].'</td></tr>';
        echo '<tr><th>'.$Lang['fax'].'</th><td>'.$obj['fax'].'</td></tr>';
        echo '<tr><th>'.$Lang['e_mail'].'</th><td>'.$obj['e_mail'].'</td></tr>';
        echo '<tr><th>'.$Lang['real_address'].'</th><td>'.$obj['real_address'].'</td></tr>';
        echo '<tr><th>'.$Lang['comment'].'</th><td>'.$obj['comment'].'</td></tr>';
        echo '<tr><th>'.$Lang['unwanted'].'</th><td><input type="checkbox"'.$unwanted.' disabled="disabled" /></td></tr>';
        echo '</table><a href="?p='.$page.'&a=edit&id='.$id.'"><img src="img/edit.png" alt="'.$Lang['edit'].'" title="'.$Lang['edit'].'" /></a>';
    break;
    case 'edit':
        if($_POST)
        {
            if(!isset($_GET['id'])) break;
            $id=getVar('id');
            if((!isset($_POST['name']) || $_POST['name']=='')&&(!isset($_POST['name2']) || $_POST['name2']=='')) {msg($Lang['error'], $Lang['nothing_to_edit'],'error'); break;}
            if((!isset($_POST['fp_code']) || $_POST['fp_code']=='')&&(!isset($_POST['fp_code2']) || $_POST['fp_code2']=='')) {msg($Lang['error'], $Lang['nothing_to_edit'],'error'); break;}
            
            $legal=isset($_POST['legal'])&&$_POST['legal']=='on'?true:false;
            $unwanted=isset($_POST['unwanted'])&&$_POST['unwanted']=='on'?true:false;
            if($legal)
            {
                $name=getVar('name2');
                $code=getVar('fp_code2','int');
                $reg_date=getVar('reg_date')==''?'NULL':'\''.getVar('reg_date').'\'';
                $fiscal=getVar('fiscal_code', 'int');
            }
            else
            {
                $name=getVar('name');
                $code=getVar('fp_code');
                $reg_date='NULL';
                $fiscal='';
            }
            $laddress=getVar('legal_address');
            $raddress=getVar('real_address');
            $phone=getVar('phone');
            $fax=getVar('fax');
            $mail=getVar('mail');
            $comment=getVar('comment');
            $legal=$legal==true?'1':'0';
            $unwanted=$unwanted==true?'1':'0';
            $sql->query("UPDATE clients SET `name`='$name', `address`='$laddress', `legal`='$legal', `code`='$code', `fiscal_code`='$fiscal', `reg_date`=$reg_date, `telephone`='$phone', `fax`='$fax', `e_mail`='$mail', `real_address`='$raddress', `comment`='$comment', `unwanted`='$unwanted' WHERE `id`='$id';");
            if($sql->row_count)
                msg($Lang['success'], $Lang['client_updated'].'<br /><a href="?p='.$page.'&a=view&id='.$id.'">'.$Lang['view_client'].'</a><br /><a href="?p='.$page.'&a=edit">'.$Lang['edit'].'</a>');
            else
                msg($Lang['warning'], $Lang['failed_to_update_or'], 'warning');
        }
        else
        {
            if(!isset($_GET['id'])) break;
            $id=getVar('id');
            $qry=$sql->query("SELECT id, name, address, reg_date, real_address, telephone, fax, e_mail, real_address, unwanted, comment, IF(legal<>0,code,fiscal_code) as fp_code, IF(LENGTH(IF(legal<>0,code,fiscal_code))=12,'{$Lang['individualU']}','{$Lang['legalU']}') as ptype FROM clients WHERE clients.id='$id'");
            $obj=$sql->fetch_array($qry);
            echo '<form action="?p='.$page.'&a=edit&id='.$id.'" method="post" onsubmit="client_submit();"><table id="client" name="client" border="1">';
            $legal=$obj['ptype']==$Lang['legalU']?' checked="checked"':'';
            $unwanted=$obj['unwanted']=='1'?' checked="checked"':'';
            $display_legal=$obj['ptype']==$Lang['legalU']? ' style="display: table-row"': ' style="display: none"';
            $display_ind=$obj['ptype']==$Lang['legalU']? ' style="display: none"': ' style="display: table-row"';
            echo '<tr'.$display_ind.'><th>'.$Lang['name'].'</th><td><input type="text" id="name" name="name" value="'.$obj['name'].'" /></td></tr>';
            echo '<tr'.$display_legal.'><th>'.$Lang['title'].'</th><td><input type="text" id="name2" name="name2" value="'.$obj['name'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['legal'].'</th><td><input id="legal" name="legal" type="checkbox"'.$legal.' onclick="toogle_client();" /></td></tr>';
            echo '<tr'.$display_ind.'><th>'.$Lang['person_code'].'</th><td><input type="text" id="fp_code" name="fp_code" value="'.$obj['fp_code'].'" /></td></tr>';
            echo '<tr'.$display_legal.'><th>'.$Lang['reg_nr'].'</th><td><input type="text" id="fp_code2" name="fp_code2" value="'.$obj['fp_code'].'" /></td></tr>';
            echo '<tr'.$display_legal.'><th>'.$Lang['reg_date'].'</th><td><input type="text" name="reg_date" value="'.$obj['reg_date'].'" /></td></tr>';
            echo '<tr'.$display_legal.'><th>'.$Lang['fiscal_code'].'</th><td><input type="text" name="fiscal_code" value="'.$obj['fiscal_code'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['legal_address'].'</th><td><input type="text" name="legal_address" value="'.$obj['address'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['phone'].'</th><td><input type="text" name="phone" value="'.$obj['telephone'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['fax'].'</th><td><input type="text" name="fax" value="'.$obj['fax'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['e_mail'].'</th><td><input type="text" name="mail" value="'.$obj['e_mail'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['real_address'].'</th><td><input type="text" name="real_address" value="'.$obj['real_address'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['comment'].'</th><td><input type="text" name="comment" value="'.$obj['comment'].'" /></td></tr>';
            echo '<tr><th>'.$Lang['unwanted'].'</th><td><input name="unwanted" type="checkbox"'.$unwanted.' /></td></tr>';
            echo '</table><input type="submit" value="'.$Lang['save'].'" /></form>';
        }
    break;
    case 'del':
        if(!isset($_GET['id'])) break;
        $id=getVar('id');
        if(isset($_GET['confirm']))
        {
            $sql->query("DELETE FROM clients WHERE id='$id';");
            if($sql->row_count)
                msg($Lang['success'], $Lang['client_deleted']);
            else
                msg($Lang['warning'], $Lang['failed_delete'], 'warning');
        }
        else
        {
            msg($Lang['warning'],$Lang['are_you_sure'].'<br /> <a href="?p='.$page.'&a=del&id='.$id.'&confirm=1">'.$Lang['yes'].'</a>/<a href="?p='.$page.'">'.$Lang['no'].'</a>"', 'warning');
            msg($Lang['warning'], $Lang['client_delete_if_no'], 'warning');
        }
    break;
    default:
        if(isset($_GET['pg']))
	       $start = getVar('pg');
        else 
        	$start = 1;
        if(!is_numeric($start) || $start == 0)
	       $start = 1;
        $start = abs($start) - 1;
        $startlimit = $start * getConfig('settings','items_per_page','100');
        $rows=getConfig('settings','items_per_page','100');
        $selectT=getVar('selectT','string',true);
        $selectV=getVar('selectV','string',true);
        
        //echo pagechoose($start + 1, $page_foot, $npage);
        echo '<a href="?p='.$page.'&a=add"><img src="img/add.png" alt="'.$Lang['add'].'" title="'.$Lang['add'].'" /></a>';
        $npage=$page;
        if($selectT !='' && $selectV!='')
        {
            $npage.='&selectT='.$selectT;
            $npage.='&selectV='.$selectV;
            $qry_add=" WHERE";
            switch($selectT)
            {
                case 'name':
                    $qry_add.=" `name` LIKE '%$selectV%'";
                break;
                case 'address':
                    $qry_add.=" `address` LIKE '%$selectV%' OR `real_address` LIKE '%$selectV%'";
                break;
                case 'pc':
                    $qry_add.=" `code` LIKE '%$selectV%' OR `fiscal_code` LIKE '%$selectV%'";
                break;
            }
        }
        $page_foot=$sql->query("SELECT count(*) FROM clients $qry_add;");
        $page_foot = $sql->result($page_foot);
    $qry=$sql->query("SELECT id, name, address, IF(legal<>0,code,fiscal_code) as fp_code, IF(LENGTH(IF(legal<>0,code,fiscal_code))=12,'{$Lang['individualU']}','{$Lang['legalU']}') as ptype FROM clients $qry_add LIMIT $startlimit, $rows");
    ?>
    <form method="get" action="index.php">
    <input type="hidden" name="p" value="<?php echo $page;?>" />
    <input type="hidden" name="pg" value="<?php echo $pg;?>" />
    <select name="selectT"><option value=""></option>
    <option value="name"<?php echo ($selectT=='name')?' selected="selected"':''; ?>><?php echo $Lang['name'];?></option>
    <option value="address"<?php echo ($selectT=='address')?' selected="selected"':''; ?>><?php echo $Lang['address'];?></option>
    <option value="pc"<?php echo ($selectT=='pc')?' selected="selected"':''; ?>><?php echo $Lang['person_code'].'/'.$Lang['reg_nr'];?></option>
    </select> contains <input name="selectV" type="text" value="<?php echo $selectV;?>" /><input type="submit" value="GO" /></form>
    <?php
    echo '<table border="1">';
    echo '<tr><th>'.$Lang['name'].'</th><th>'.$Lang['address'].'</th><th>'.$Lang['person_code'].'/'.$Lang['reg_nr'].'</th><th>'.$Lang['legal'].'/'.$Lang['individual'].'</th><th>'.$Lang['actions'].'</th></tr>';
    while($obj=$sql->fetch_array($qry))
    {
        echo '<tr><td>'.$obj['name'].'</td><td>'.$obj['address'].'</td><td>'.$obj['fp_code'].'</td><td>'.$obj['ptype'].'</td><td><a href="?p='.$page.'&a=view&id='.$obj['id'].'&no_head" target="_blank"><img src="img/view.png" alt="'.$Lang['view'].'" title="'.$Lang['view'].'" /></a><a href="?p='.$page.'&a=edit&id='.$obj['id'].'"><img src="img/edit.png" alt="'.$Lang['edit'].'" title="'.$Lang['edit'].'" /></a><a href="?p='.$page.'&a=del&id='.$obj['id'].'"><img src="img/delete.png" alt="'.$Lang['delete'].'" title="'.$Lang['delete'].'" /></a></td></tr>';
    }
    echo '</table>';

    
        echo pagechoose($start + 1, $page_foot, $npage);
    break;
}
?>